using System;
using System.Buffers;
using System.IO;
using SharpCompress.Compressors.LZMA;
using Xunit;

namespace SharpCompress.Test.Streams;

public class LzmaStreamTests
{
    [Fact]
    public void TestLzma2Decompress1Byte()
    {
        var properties = new byte[] { 0x01 };
        var compressedData = new byte[] { 0x01, 0x00, 0x00, 0x58, 0x00 };
        var lzma2Stream = new MemoryStream(compressedData);

        var decompressor = new LzmaStream(properties, lzma2Stream, 5, 1);
        Assert.Equal('X', decompressor.ReadByte());
    }

    private static byte[] LzmaData { get; } =
    [
        0x5D,
        0x00,
        0x20,
        0x00,
        0x00,
        0x48,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x80,
        0x24,
        0x18,
        0x2F,
        0xEB,
        0x20,
        0x78,
        0xBA,
        0x78,
        0x70,
        0xDC,
        0x43,
        0x2C,
        0x32,
        0xC9,
        0xC3,
        0x97,
        0x4D,
        0x10,
        0x74,
        0xE2,
        0x20,
        0xBF,
        0x5A,
        0xB4,
        0xB3,
        0xC4,
        0x31,
        0x80,
        0x26,
        0x3E,
        0x6A,
        0xEA,
        0x51,
        0xFC,
        0xE4,
        0x8D,
        0x54,
        0x96,
        0x05,
        0xCC,
        0x78,
        0x59,
        0xAC,
        0xD4,
        0x21,
        0x65,
        0x8F,
        0xA9,
        0xC8,
        0x0D,
        0x9B,
        0xE2,
        0xC2,
        0xF9,
        0x7C,
        0x3C,
        0xDD,
        0x4D,
        0x38,
        0x04,
        0x0B,
        0xF8,
        0x0B,
        0x68,
        0xA5,
        0x93,
        0x6C,
        0x64,
        0xAC,
        0xCF,
        0x71,
        0x68,
        0xE8,
        0x69,
        0x25,
        0xC6,
        0x17,
        0x28,
        0xF1,
        0x7C,
        0xF1,
        0xDC,
        0x47,
        0x51,
        0x4D,
        0x1E,
        0x0E,
        0x0B,
        0x80,
        0x37,
        0x24,
        0x58,
        0x80,
        0xF7,
        0xB4,
        0xAC,
        0x54,
        0xF1,
        0x0F,
        0x7F,
        0x0F,
        0x0F,
        0xF5,
        0x9C,
        0xDE,
        0x54,
        0x4F,
        0xA3,
        0x7B,
        0x20,
        0xC5,
        0xA8,
        0x18,
        0x3B,
        0xED,
        0xDC,
        0x04,
        0xF6,
        0xFB,
        0x86,
        0xE0,
        0xAB,
        0xB6,
        0x87,
        0x99,
        0x92,
        0x43,
        0x7B,
        0x2C,
        0xCC,
        0x31,
        0x83,
        0x90,
        0xFF,
        0xF1,
        0x76,
        0x03,
        0x90,
    ];

    /// <summary>
    /// The decoded data for <see cref="LzmaData"/>.
    /// </summary>
    private static byte[] LzmaResultData { get; } =
    [
        0x01,
        0x00,
        0xFD,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0xFA,
        0x61,
        0x18,
        0x5F,
        0x02,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x02,
        0x00,
        0x00,
        0x00,
        0x03,
        0x00,
        0x00,
        0x00,
        0x01,
        0x00,
        0x00,
        0x00,
        0x02,
        0x00,
        0xB4,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x3D,
        0x61,
        0xE5,
        0x5E,
        0x03,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x12,
        0x00,
        0x00,
        0x00,
        0x02,
        0x00,
        0xB4,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0xE2,
        0x61,
        0x18,
        0x5F,
        0x04,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x12,
        0x00,
        0x00,
        0x00,
        0x29,
        0x00,
        0x00,
        0x00,
        0x01,
        0x00,
        0xFD,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x14,
        0x62,
        0x18,
        0x5F,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x03,
        0x00,
        0x00,
        0x00,
        0x40,
        0x00,
        0x00,
        0x00,
        0x09,
        0x00,
        0x00,
        0x00,
        0x02,
        0x00,
        0xB4,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x7F,
        0x61,
        0xE5,
        0x5E,
        0x05,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x3B,
        0x00,
        0x00,
        0x00,
        0xCB,
        0x15,
        0x00,
        0x00,
        0x02,
        0x00,
        0xB4,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x7F,
        0x61,
        0xE5,
        0x5E,
        0x06,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x3B,
        0x00,
        0x00,
        0x00,
        0xCB,
        0x15,
        0x00,
        0x00,
        0x02,
        0x00,
        0xB4,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x3D,
        0x61,
        0xE5,
        0x5E,
        0x07,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x12,
        0x00,
        0x00,
        0x00,
        0x02,
        0x00,
        0xB4,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0xFC,
        0x96,
        0x40,
        0x5C,
        0x08,
        0x00,
        0x00,
        0x00,
        0x60,
        0x00,
        0x00,
        0x00,
        0xFF,
        0xFF,
        0xFF,
        0xFF,
        0x00,
        0x00,
        0x00,
        0x00,
        0xF8,
        0x83,
        0x12,
        0x00,
        0xD4,
        0x99,
        0x00,
        0x00,
        0x43,
        0x95,
        0x00,
        0x00,
        0xEB,
        0x7A,
        0x00,
        0x00,
        0x40,
        0x6F,
        0x00,
        0x00,
        0xD2,
        0x6F,
        0x00,
        0x00,
        0x67,
        0x74,
        0x00,
        0x00,
        0x02,
        0x69,
        0x00,
        0x00,
        0x76,
        0x79,
        0x00,
        0x00,
        0x98,
        0x66,
        0x00,
        0x00,
        0x23,
        0x25,
        0x00,
        0x00,
        0x01,
        0x00,
        0xFD,
        0x01,
        0x00,
        0x00,
        0x00,
        0x00,
        0x3B,
        0x2F,
        0xC0,
        0x5F,
        0x09,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x03,
        0x00,
        0x00,
        0x00,
        0x69,
        0x00,
        0x3D,
        0x00,
        0x0A,
        0x00,
        0x00,
        0x00,
    ];

    [Fact]
    public void TestLzmaBuffer()
    {
        var input = new MemoryStream(LzmaData);
        using var output = new MemoryStream();
        var properties = new byte[5];
        input.Read(properties, 0, 5);

        var fileLengthBytes = new byte[8];
        input.Read(fileLengthBytes, 0, 8);
        var fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

        var coder = new Decoder();
        coder.SetDecoderProperties(properties);
        coder.Code(input, output, input.Length, fileLength, null);

        Assert.Equal(output.ToArray(), LzmaResultData);
    }

    [Fact]
    public void TestLzmaStreamEncodingWritesData()
    {
        using var inputStream = new MemoryStream(LzmaResultData);
        using MemoryStream outputStream = new();
        using var lzmaStream = new LzmaStream(LzmaEncoderProperties.Default, false, outputStream);
        inputStream.CopyTo(lzmaStream);
        lzmaStream.Close();
        Assert.NotEqual(0, outputStream.Length);
    }

    [Fact]
    public void TestLzmaEncodingAccuracy()
    {
        var input = new MemoryStream(LzmaResultData);
        var compressed = new MemoryStream();
        var lzmaEncodingStream = new LzmaStream(LzmaEncoderProperties.Default, false, compressed);
        input.CopyTo(lzmaEncodingStream);
        lzmaEncodingStream.Close();
        compressed.Position = 0;

        var output = new MemoryStream();
        DecompressLzmaStream(
            lzmaEncodingStream.Properties,
            compressed,
            compressed.Length,
            output,
            LzmaResultData.LongLength
        );

        Assert.Equal(output.ToArray(), LzmaResultData);
    }

    private static void DecompressLzmaStream(
        byte[] properties,
        Stream compressedStream,
        long compressedSize,
        Stream decompressedStream,
        long decompressedSize
    )
    {
        var lzmaStream = new LzmaStream(
            properties,
            compressedStream,
            compressedSize,
            -1,
            null,
            false
        );

        var buffer = new byte[1024];
        long totalRead = 0;
        while (totalRead < decompressedSize)
        {
            var toRead = (int)Math.Min(buffer.Length, decompressedSize - totalRead);
            var read = lzmaStream.Read(buffer, 0, toRead);
            if (read > 0)
            {
                decompressedStream.Write(buffer, 0, read);
                totalRead += read;
            }
            else
            {
                break;
            }
        }
    }
}
